package io.jee.medusa.orm.condition;

import java.util.ArrayList;
import java.util.List;

public class ConditionGroup {
	// >>>>>>>>>>>>>>>>>>>>>>>业务方法区<<<<<<<<<<<<<<<<<<<<<<<<<<
	
	/**
	 * 与（and）方式新增条件
	 * @param condition
	 */
	public void and(Condition condition) {
		condition.setRelation(Condition.RELATION_AND);
		addCondition(condition);
	}

	/**
	 * 与（and）方式新增条件组
	 * @param group
	 */
	public void and(ConditionGroup group) {
		group.setRelation(Condition.RELATION_AND);
		addGroups(group);
	}

	/**
	 * 或（or）方式新增条件
	 * @param condition
	 */
	public void or(Condition condition) {
		condition.setRelation(Condition.RELATION_OR);
		addCondition(condition);
	}

	/**
	 * 或（or）方式新增条件组
	 * @param group
	 */
	public void or(ConditionGroup group) {
		group.setRelation(Condition.RELATION_OR);
		addGroups(group);
	}
	
	public void clear () {
		conditions.clear();
		groups.clear();
	}

	private void addCondition(Condition condition) {
		if (conditions.size() == 0) {
			condition.setFirst(true);
		}
		conditions.add(condition);
	}

	private void addGroups(ConditionGroup group) {
		groups.add(group);
	}

	// >>>>>>>>>>>>>>>>>>>>>>>私有属性区<<<<<<<<<<<<<<<<<<<<<<<<<<
	private Integer relation = Condition.RELATION_AND;
	private List<Condition> conditions = new ArrayList<Condition>();
	private List<ConditionGroup> groups = new ArrayList<ConditionGroup>();

	public Integer getRelation() {
		return relation;
	}

	public void setRelation(Integer relation) {
		this.relation = relation;
	}

	public List<Condition> getConditions() {
		return conditions;
	}

	public void setConditions(List<Condition> conditions) {
		this.conditions = conditions;
	}

	public List<ConditionGroup> getGroups() {
		return groups;
	}

	public void setGroups(List<ConditionGroup> groups) {
		this.groups = groups;
	}

}
